#include <Animation.hpp>

#include <iostream>

Modeling::Animation::~Animation()
{}

Modeling::Animation::Animation(unsigned int frames, unsigned int FPS, unsigned int loops)
{
	this->stop();
	this->setFrames(frames);
	this->setFPS(FPS);
	this->setLoops(loops);
}

unsigned int Modeling::Animation::getFrames()
{
	return this->frames;
}

void Modeling::Animation::setFrames(unsigned int frames)
{
	this->frames = frames;
}

unsigned int Modeling::Animation::getFPS()
{
	return this->FPS;
}

void Modeling::Animation::setFPS(unsigned int FPS)
{
	this->FPS = FPS;
	this->CPF = (clock_t)(CLOCKS_PER_SEC / FPS);
}

unsigned int Modeling::Animation::getLoops()
{
	return this->loops;
}

void Modeling::Animation::setLoops(unsigned int loops)
{
	this->loops = loops;
}

void Modeling::Animation::play()
{
	this->T0 = clock();
	this->status = PLAYING;
}

void Modeling::Animation::pause()
{
	this->status = PAUSED;
}

void Modeling::Animation::stop()
{
	this->setCurrentFrame(0);
	this->status = STOPED;
}

bool Modeling::Animation::isPlaying()
{
	return this->status == PLAYING;
}

bool Modeling::Animation::isPaused()
{
	return this->status == PAUSED;
}

bool Modeling::Animation::isStoped()
{
	return this->status == STOPED;
}

unsigned int Modeling::Animation::getCurrentFrame()
{
	if (this->isPlaying()) {
		elapsedClocks = clock() - T0;
		if (elapsedClocks >= this->CPF) {
			elapsedFrames = (unsigned int)(elapsedClocks / CPF);
			this->currentFrame = (this->currentFrame + elapsedFrames) % this->frames;
			T0 = clock();
		}
	}

	return currentFrame;
}

void Modeling::Animation::setCurrentFrame(unsigned int frame)
{
	this->currentFrame = frame;
}
